
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>元编程 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="umbrella-projects.html" />
    
    
    <link rel="prev" href="otp-distribution.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">第一部分：基础</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../basics/basics.html">
            
                <a href="../basics/basics.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../basics/collections.html">
            
                <a href="../basics/collections.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../basics/enum.html">
            
                <a href="../basics/enum.html">
            
                    
                    Enum 模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../basics/pattern-matching.html">
            
                <a href="../basics/pattern-matching.html">
            
                    
                    模式匹配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../basics/control-structures.html">
            
                <a href="../basics/control-structures.html">
            
                    
                    控制语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../basics/functions.html">
            
                <a href="../basics/functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../basics/pipe-operator.html">
            
                <a href="../basics/pipe-operator.html">
            
                    
                    管道操作符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../basics/modules.html">
            
                <a href="../basics/modules.html">
            
                    
                    模块(Module)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../basics/mix.html">
            
                <a href="../basics/mix.html">
            
                    
                    Mix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../basics/sigils.html">
            
                <a href="../basics/sigils.html">
            
                    
                    魔符(Sigil)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../basics/documentation.html">
            
                <a href="../basics/documentation.html">
            
                    
                    文档模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../basics/testing.html">
            
                <a href="../basics/testing.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../basics/comprehensions.html">
            
                <a href="../basics/comprehensions.html">
            
                    
                    推导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../basics/strings.html">
            
                <a href="../basics/strings.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../basics/date-time.html">
            
                <a href="../basics/date-time.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../basics/mix-tasks.html">
            
                <a href="../basics/mix-tasks.html">
            
                    
                    自定义Mix任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../basics/iex-helpers.html">
            
                <a href="../basics/iex-helpers.html">
            
                    
                    IEx辅助函数
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第二部分：高级</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="erlang.html">
            
                <a href="erlang.html">
            
                    
                    和Erlang互操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="error-handling.html">
            
                <a href="error-handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="escripts.html">
            
                <a href="escripts.html">
            
                    
                    可执行文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="concurrency.html">
            
                <a href="concurrency.html">
            
                    
                    并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="otp-concurrency.html">
            
                <a href="otp-concurrency.html">
            
                    
                    OTP并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="otp-supervisors.html">
            
                <a href="otp-supervisors.html">
            
                    
                    OTP Supervisors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="otp-distribution.html">
            
                <a href="otp-distribution.html">
            
                    
                    OTP 分布式
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="2.8" data-path="metaprogramming.html">
            
                <a href="metaprogramming.html">
            
                    
                    元编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="umbrella-projects.html">
            
                <a href="umbrella-projects.html">
            
                    
                    Umbrella Projects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="typespec.html">
            
                <a href="typespec.html">
            
                    
                    Specifications and types
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="behaviours.html">
            
                <a href="behaviours.html">
            
                    
                    行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="gen-stage.html">
            
                <a href="gen-stage.html">
            
                    
                    GenStage
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="protocols.html">
            
                <a href="protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="nerves.html">
            
                <a href="nerves.html">
            
                    
                    Nerves
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第三部分：ECTO</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../ecto/basics.html">
            
                <a href="../ecto/basics.html">
            
                    
                    Basics
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../ecto/changesets.html">
            
                <a href="../ecto/changesets.html">
            
                    
                    Changesets
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../ecto/associations.html">
            
                <a href="../ecto/associations.html">
            
                    
                    关联关系
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../ecto/querying.html">
            
                <a href="../ecto/querying.html">
            
                    
                    查询
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第四部分：专题</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../specifics/plug.html">
            
                <a href="../specifics/plug.html">
            
                    
                    Plug
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../specifics/eex.html">
            
                <a href="../specifics/eex.html">
            
                    
                    嵌入的 Elixir(EEx)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../specifics/ets.html">
            
                <a href="../specifics/ets.html">
            
                    
                    Erlang 项式存储(ETS)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../specifics/mnesia.html">
            
                <a href="../specifics/mnesia.html">
            
                    
                    Mnesia 数据库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../specifics/debugging.html">
            
                <a href="../specifics/debugging.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第五部分：程序库</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../libraries/guardian.html">
            
                <a href="../libraries/guardian.html">
            
                    
                    Guardian(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../libraries/poolboy.html">
            
                <a href="../libraries/poolboy.html">
            
                    
                    Poolboy
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../libraries/benchee.html">
            
                <a href="../libraries/benchee.html">
            
                    
                    Benchee
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../libraries/bypass.html">
            
                <a href="../libraries/bypass.html">
            
                    
                    Bypass
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="../libraries/distillery.html">
            
                <a href="../libraries/distillery.html">
            
                    
                    Distillery(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../libraries/stream-data.html">
            
                <a href="../libraries/stream-data.html">
            
                    
                    StreamData
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >元编程</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>&#x5143;&#x7F16;&#x7A0B;&#x662F;&#x6307;&#x7528;&#x4EE3;&#x7801;&#x6765;&#x5199;&#x4EE3;&#x7801;&#x7684;&#x8FC7;&#x7A0B;&#x3002;&#x5728; Elixir &#x4E2D;&#xFF0C;&#x8FD9;&#x8BF4;&#x660E;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x6269;&#x5C55;&#x8BE5;&#x8BED;&#x8A00;&#xFF0C;&#x52A8;&#x6001;&#x5730;&#x4FEE;&#x6539;&#x8BE5;&#x8BED;&#x8A00;&#x6765;&#x6EE1;&#x8DB3;&#x6211;&#x4EEC;&#x7684;&#x9700;&#x6C42;&#x3002;&#x6211;&#x4EEC;&#x4F1A;&#x5148;&#x770B;&#x770B; Elixir &#x5E95;&#x5C42;&#x662F;&#x600E;&#x4E48;&#x5B9E;&#x73B0;&#x7684;&#xFF0C;&#x7136;&#x540E;&#x8BB2;&#x600E;&#x4E48;&#x4FEE;&#x6539;&#x5B83;&#xFF0C;&#x6700;&#x540E;&#x4F1A;&#x4F7F;&#x7528;&#x521A;&#x5B66;&#x8FC7;&#x7684;&#x77E5;&#x8BC6;&#x6765;&#x6269;&#x5C55;&#x5B83;&#x3002;  </p>
<p>&#x5FE0;&#x544A;&#xFF1A;&#x5143;&#x7F16;&#x7A0B;&#x4E0D;&#x5BB9;&#x6613;&#x7528;&#x597D;&#xFF0C;&#x53EA;&#x6709;&#x5728;&#x7EDD;&#x5BF9;&#x9700;&#x8981;&#x7684;&#x65F6;&#x5019;&#x624D;&#x53BB;&#x4F7F;&#x7528;&#x5B83;&#x3002;&#x8FC7;&#x5EA6;&#x4F7F;&#x7528;&#x5143;&#x7F16;&#x7A0B;&#x4F1A;&#x5BFC;&#x81F4;&#x4EE3;&#x7801;&#x5F88;&#x590D;&#x6742;&#xFF0C;&#x4E0D;&#x5BB9;&#x6613;&#x7406;&#x89E3;&#x548C;&#x8C03;&#x8BD5;&#x3002;  </p>
<h2 id="quote">Quote</h2>
<p>&#x5B66;&#x4E60;&#x5143;&#x7F16;&#x7A0B;&#x7684;&#x7B2C;&#x4E00;&#x6B65;&#x662F;&#x7406;&#x89E3;&#x8868;&#x8FBE;&#x5F0F;&#x662F;&#x600E;&#x4E48;&#x8868;&#x793A;&#x7684;&#x3002;Elixir &#x7684; AST&#xFF08;&#x62BD;&#x8C61;&#x8BED;&#x6CD5;&#x6811;&#xFF09;&#x662F;&#x7531;&#x5143;&#x7EC4;&#x6784;&#x6210;&#x7684;&#x3002;&#x8FD9;&#x4E9B;&#x5143;&#x7EC4;&#x5305;&#x542B;&#x4E86;&#x4E09;&#x4E2A;&#x90E8;&#x5206;&#xFF1A;&#x51FD;&#x6570;&#x540D;&#x79F0;&#xFF0C;metadata&#xFF0C;&#x8FD8;&#x6709;&#x51FD;&#x6570;&#x7684;&#x53C2;&#x6570;&#x3002;  </p>
<p>Elixir &#x63D0;&#x4F9B;&#x4E86; <code>quote/2</code> &#x51FD;&#x6570;&#xFF0C;&#x53EF;&#x4EE5;&#x8BA9;&#x6211;&#x4EEC;&#x770B;&#x5230;&#x8FD9;&#x4E9B;&#x5185;&#x90E8;&#x7ED3;&#x6784;&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;<code>quote/2</code> &#x80FD;&#x628A; Elixir &#x4EE3;&#x7801;&#x8F6C;&#x6362;&#x6210;&#x4EE3;&#x7801;&#x7684;&#x5E95;&#x5C42;&#x8868;&#x793A;&#x5F62;&#x5F0F;&#x3002;  </p>
<pre><code class="lang-elixir">iex&gt; <span class="hljs-keyword">quote</span> <span class="hljs-symbol">do:</span> <span class="hljs-number">42</span>
<span class="hljs-number">42</span>
iex&gt; <span class="hljs-keyword">quote</span> <span class="hljs-symbol">do:</span> <span class="hljs-string">&quot;Hello&quot;</span>
<span class="hljs-string">&quot;Hello&quot;</span>
iex&gt; <span class="hljs-keyword">quote</span> <span class="hljs-symbol">do:</span> <span class="hljs-symbol">:world</span>
<span class="hljs-symbol">:world</span>
iex&gt; <span class="hljs-keyword">quote</span> <span class="hljs-symbol">do:</span> <span class="hljs-number">1</span> + <span class="hljs-number">2</span>
{<span class="hljs-symbol">:+</span>, [<span class="hljs-symbol">context:</span> Elixir, <span class="hljs-symbol">import:</span> Kernel], [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>]}
iex&gt; <span class="hljs-keyword">quote</span> <span class="hljs-symbol">do:</span> if value, <span class="hljs-symbol">do:</span> <span class="hljs-string">&quot;True&quot;</span>, <span class="hljs-symbol">else:</span> <span class="hljs-string">&quot;False&quot;</span>
{<span class="hljs-symbol">:if</span>, [<span class="hljs-symbol">context:</span> Elixir, <span class="hljs-symbol">import:</span> Kernel],
 [{<span class="hljs-symbol">:value</span>, [], Elixir}, [<span class="hljs-symbol">do:</span> <span class="hljs-string">&quot;True&quot;</span>, <span class="hljs-symbol">else:</span> <span class="hljs-string">&quot;False&quot;</span>]]}
</code></pre>
<p>&#x6CE8;&#x610F;&#x5230;&#x524D;&#x9762;&#x4E09;&#x4E2A;&#x5E76;&#x6CA1;&#x6709;&#x8FD4;&#x56DE;&#x5217;&#x8868;&#x4E86;&#x5417;&#xFF1F;&#x8FD9;&#x4E94;&#x79CD;&#x5B57;&#x9762;&#x91CF;&#xFF08;literal&#xFF09;&#x4F7F;&#x7528; quote &#x7684;&#x8FD4;&#x56DE;&#x503C;&#x662F;&#x5B83;&#x4EEC;&#x81EA;&#x8EAB;&#xFF1A;  </p>
<pre><code class="lang-elixir">iex&gt; <span class="hljs-symbol">:atom</span>
<span class="hljs-symbol">:atom</span>
iex&gt; <span class="hljs-string">&quot;string&quot;</span>
<span class="hljs-string">&quot;string&quot;</span>
iex&gt; <span class="hljs-number">1</span> <span class="hljs-comment"># All numbers</span>
<span class="hljs-number">1</span>
iex&gt; [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>] <span class="hljs-comment"># Lists</span>
[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>]
iex&gt; {<span class="hljs-string">&quot;hello&quot;</span>, <span class="hljs-symbol">:world</span>} <span class="hljs-comment"># 2 element tuples</span>
{<span class="hljs-string">&quot;hello&quot;</span>, <span class="hljs-symbol">:world</span>}
</code></pre>
<h2 id="unquote">Unquote</h2>
<p>&#x77E5;&#x9053;&#x4E86;&#x5982;&#x4F55;&#x83B7;&#x53D6;&#x4EE3;&#x7801;&#x7684;&#x5185;&#x90E8;&#x8868;&#x793A;&#xFF0C;&#x90A3;&#x600E;&#x4E48;&#x4FEE;&#x6539;&#x5B83;&#x5462;&#xFF1F;&#x6211;&#x4EEC;&#x4F9D;&#x9760; <code>unquote/1</code> &#x6765;&#x63D2;&#x5165;&#x65B0;&#x7684;&#x4EE3;&#x7801;&#x548C;&#x503C;&#x3002;&#x5F53;&#x6211;&#x4EEC; unquote &#x4E00;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4F1A;&#x628A;&#x5B83;&#x8FD0;&#x884C;&#x7684;&#x7ED3;&#x679C;&#x63D2;&#x5165;&#x5230; AST&#x3002;&#x6211;&#x4EEC;&#x6765;&#x770B;&#x4E2A;&#x4F8B;&#x5B50;&#x7406;&#x89E3;&#x4E00;&#x4E0B; <code>unquote/2</code> &#x7684;&#x7528;&#x6CD5;:  </p>
<pre><code class="lang-elixir">iex&gt; denominator = <span class="hljs-number">2</span>
<span class="hljs-number">2</span>
iex&gt; <span class="hljs-keyword">quote</span> <span class="hljs-symbol">do:</span> divide(<span class="hljs-number">42</span>, denominator)
{<span class="hljs-symbol">:divide</span>, [], [<span class="hljs-number">42</span>, {<span class="hljs-symbol">:denominator</span>, [], Elixir}]}
iex&gt; <span class="hljs-keyword">quote</span> <span class="hljs-symbol">do:</span> divide(<span class="hljs-number">42</span>, unquote(denominator))
{<span class="hljs-symbol">:divide</span>, [], [<span class="hljs-number">42</span>, <span class="hljs-number">2</span>]}
</code></pre>
<p>&#x5728;&#x7B2C;&#x4E00;&#x4E2A;&#x7684;&#x4F8B;&#x5B50;&#x91CC;&#xFF0C;&#x53D8;&#x91CF; <code>denominator</code> &#x5728; quote &#x7684;&#x65F6;&#x5019;&#x4F1A;&#x5BFC;&#x81F4;&#x751F;&#x6210;&#x7684; AST &#x5305;&#x542B;&#x4E86;&#x4E00;&#x4E2A;&#x8BBF;&#x95EE;&#x4E86;&#x8FD9;&#x4E2A;&#x53D8;&#x91CF;&#x7684;&#x5217;&#x8868;&#x3002;&#x800C;&#x5728;&#x4F7F;&#x7528; <code>unquote/1</code> &#x7684;&#x4F8B;&#x5B50;&#x91CC;&#xFF0C;&#x751F;&#x6210;&#x7684;&#x4EE3;&#x7801;&#x5C31;&#x76F4;&#x63A5;&#x5305;&#x542B;&#x4E86; <code>denominator</code> &#x7684;&#x503C;&#x3002;  </p>
<h2 id="&#x5B8F;">&#x5B8F;</h2>
<p>&#x7406;&#x89E3;&#x4E86; <code>quote/2</code> &#x548C; <code>unquote/1</code>&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x5F00;&#x59CB;&#x6DF1;&#x5165;&#x5B66;&#x4E60;&#x5B8F;&#x4E86;&#x3002;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#xFF1A;&#x5B8F;&#x548C;&#x5143;&#x7F16;&#x7A0B;&#x4E00;&#x6837;&#xFF0C;&#x5FC5;&#x987B;&#x8C28;&#x614E;&#x4F7F;&#x7528;&#x3002;  </p>
<p>&#x7B80;&#x5355;&#x6765;&#x8BF4;&#xFF0C;&#x5B8F;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x7279;&#x522B;&#x7684;&#x51FD;&#x6570;&#xFF1A;&#x5B83;&#x8FD4;&#x56DE;&#x7684; quoted &#x7684;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x4F1A;&#x88AB;&#x63D2;&#x5165;&#x5230;&#x5E94;&#x7528;&#x7684;&#x4EE3;&#x7801;&#x4E2D;&#x3002;&#x53EF;&#x4EE5;&#x60F3;&#x8C61;&#x5B8F;&#x88AB; quoted &#x540E;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x66FF;&#x4EE3;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x50CF;&#x51FD;&#x6570;&#x90A3;&#x6837;&#x88AB;&#x8C03;&#x7528;&#x3002;&#x6709;&#x4E86;&#x5B8F;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x80FD;&#x591F;&#x6269;&#x5C55; Elixir &#x548C;&#x52A8;&#x6001;&#x5730;&#x4E3A;&#x5E94;&#x7528;&#x6DFB;&#x52A0;&#x4EE3;&#x7801;&#x4E86;&#x3002;  </p>
<p>&#x5B9A;&#x4E49;&#x5B8F;&#x9700;&#x8981;&#x7528; <code>defmacro/2</code>&#xFF0C;&#x8FD9;&#x5728; Elixir &#x4E2D;&#x4E5F;&#x662F;&#x4E00;&#x4E2A;&#x5B8F;(&#x82B1;&#x70B9;&#x65F6;&#x95F4;&#x6162;&#x6162;&#x9886;&#x4F1A;)&#x3002;&#x5728;&#x4EE5;&#x4E0B;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x4F1A;&#x5B9E;&#x73B0; <code>unless</code> &#x8FD9;&#x4E2A;&#x5B8F;&#x3002;&#x8BB0;&#x4F4F;&#xFF0C;&#x5B8F;&#x9700;&#x8981;&#x8FD4;&#x56DE; quoted &#x540E;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#xFF1A;  </p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">OurMacro</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">defmacro</span> <span class="hljs-title">unless</span></span>(expr, <span class="hljs-symbol">do:</span> block) <span class="hljs-keyword">do</span>
    <span class="hljs-keyword">quote</span> <span class="hljs-keyword">do</span>
      if !unquote(expr), <span class="hljs-symbol">do:</span> unquote(block)
    <span class="hljs-keyword">end</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x5BFC;&#x5165;&#x6A21;&#x5757;&#xFF0C;&#x6D4B;&#x8BD5;&#x4E00;&#x4E0B;&#x6211;&#x4EEC;&#x521A;&#x5B9A;&#x4E49;&#x7684;&#x5B8F;&#xFF1A;  </p>
<pre><code class="lang-elixir">iex&gt; require OurMacro
<span class="hljs-keyword">nil</span>
iex&gt; OurMacro.<span class="hljs-keyword">unless</span> <span class="hljs-keyword">true</span>, <span class="hljs-symbol">do:</span> <span class="hljs-string">&quot;Hi&quot;</span>
<span class="hljs-keyword">nil</span>
iex&gt; OurMacro.<span class="hljs-keyword">unless</span> <span class="hljs-keyword">false</span>, <span class="hljs-symbol">do:</span> <span class="hljs-string">&quot;Hi&quot;</span>
<span class="hljs-string">&quot;Hi&quot;</span>
</code></pre>
<p>&#x56E0;&#x4E3A;&#x5B8F;&#x4F1A;&#x66FF;&#x6362;&#x5E94;&#x7528;&#x4E2D;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x80FD;&#x63A7;&#x5236;&#x628A;&#x4EE3;&#x7801;&#x66FF;&#x6362;&#x6210;&#x4EC0;&#x4E48;&#xFF0C;&#x4E5F;&#x80FD;&#x63A7;&#x5236;&#x5728;&#x4EC0;&#x4E48;&#x60C5;&#x51B5;&#x4E0B;&#x8FDB;&#x884C;&#x66FF;&#x6362;&#x3002;<code>Logger</code> &#x6A21;&#x5757;&#x5C31;&#x662F;&#x5F88;&#x597D;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x5982;&#x679C; logging &#x88AB;&#x7981;&#x6B62;&#x4E86;&#xFF0C;&#x5C31;&#x4E0D;&#x4F1A;&#x6709;&#x4EE3;&#x7801;&#x88AB;&#x63D2;&#x5165;&#xFF0C;&#x6700;&#x7EC8;&#x7684;&#x4EE3;&#x7801;&#x4E5F;&#x4E0D;&#x4F1A;&#x6709;&#x8C03;&#x7528; logging &#x7684;&#x903B;&#x8F91;&#x3002;&#x548C;&#x5176;&#x4ED6;&#x8BED;&#x8A00;&#x7684;&#x533A;&#x522B;&#x5728;&#x4E8E;&#xFF1A;&#x5176;&#x4ED6;&#x8BED;&#x8A00;&#x5728; logging &#x88AB;&#x7981;&#x6B62;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x8FD8;&#x4F1A;&#x6709; logging &#x76F8;&#x5173;&#x7684;&#x4EE3;&#x7801;&#x5B58;&#x5728;&#xFF0C;&#x5373;&#x4F7F;&#x91CC;&#x9762;&#x7684;&#x5B9E;&#x73B0;&#x662F;&#x7A7A;&#x7684;&#x3002;  </p>
<p>&#x6211;&#x4EEC;&#x5199;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684; logger &#x6765;&#x8BF4;&#x660E;&#x8FD9;&#x4E00;&#x70B9;&#x3002;&#x6211;&#x4EEC;&#x7684; logger &#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x6765;&#x5F00;&#x542F;&#x6216;&#x7981;&#x6B62;&#xFF1A;  </p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Logger</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">defmacro</span> <span class="hljs-title">log</span></span>(msg) <span class="hljs-keyword">do</span>
    if Application.get_env(<span class="hljs-symbol">:logger</span>, <span class="hljs-symbol">:enabled</span>) <span class="hljs-keyword">do</span>
      <span class="hljs-keyword">quote</span> <span class="hljs-keyword">do</span>
        IO.puts(<span class="hljs-string">&quot;Logged message: <span class="hljs-subst">#{unquote(msg)}</span>&quot;</span>)
      <span class="hljs-keyword">end</span>
    <span class="hljs-keyword">end</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>

<span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span> <span class="hljs-keyword">do</span>
  require Logger

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test</span></span> <span class="hljs-keyword">do</span>
    Logger.log(<span class="hljs-string">&quot;This is a log message&quot;</span>)
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x5F53; logging &#x5F00;&#x542F;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x6211;&#x4EEC;&#x7684; <code>test</code> &#x51FD;&#x6570;&#x7684;&#x4EE3;&#x7801;&#x4F1A;&#x662F;&#x4E0B;&#x9762;&#x7684;&#x6837;&#x5B50;&#xFF1A;  </p>
<pre><code class="lang-elixir"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test</span></span> <span class="hljs-keyword">do</span>
  IO.puts(<span class="hljs-string">&quot;Logged message: <span class="hljs-subst">#{<span class="hljs-string">&quot;This is a log message&quot;</span>}</span>&quot;</span>)
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x7981;&#x6B62;&#x4E86; logging&#xFF0C;&#x6700;&#x7EC8;&#x7684;&#x4EE3;&#x7801;&#x5C31;&#x53D8;&#x6210;&#x4E86;&#xFF1A;  </p>
<pre><code class="lang-elixir"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test</span></span> <span class="hljs-keyword">do</span>
<span class="hljs-keyword">end</span>
</code></pre>
<h2 id="&#x8C03;&#x8BD5;">&#x8C03;&#x8BD5;</h2>
<p>&#x597D;&#x4E86;&#xFF0C;&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x77E5;&#x9053;&#x5982;&#x4F55;&#x4F7F;&#x7528; <code>quote/2</code>, <code>unquote/1</code> &#x548C;&#x7F16;&#x5199;&#x5B8F;&#x4E86;&#x3002;&#x4F46;&#x662F;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x9762;&#x5BF9;&#x7684;&#x662F;&#x4E00;&#x5927;&#x6BB5; quoted &#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x5E76;&#x5E0C;&#x671B;&#x7406;&#x89E3;&#x5B83;&#x600E;&#x4E48;&#x529E;&#xFF1F;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>Macro.to_string/2</code>&#x3002;&#x6765;&#x770B;&#x770B;&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#xFF1A;  </p>
<pre><code class="lang-elixir">iex&gt; Macro.to_string(<span class="hljs-keyword">quote</span>(<span class="hljs-symbol">do:</span> foo.bar(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)))
<span class="hljs-string">&quot;foo.bar(1, 2, 3)&quot;</span>
</code></pre>
<p>&#x5F53;&#x4F60;&#x60F3;&#x67E5;&#x770B;&#x901A;&#x8FC7;&#x5B8F;&#x751F;&#x6210;&#x51FA;&#x6765;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x7ED3;&#x5408; <code>Macro.expand/2</code> &#x548C; <code>Macro.expand_once/2</code> &#x7684;&#x4F7F;&#x7528;&#xFF0C;&#x8FD9;&#x4E9B;&#x51FD;&#x6570;&#x4F1A;&#x628A;&#x63D0;&#x4F9B;&#x7ED9;&#x5B8F;&#x7684;&#x539F;&#x6765;&#x7684; quoted &#x7684;&#x4EE3;&#x7801;&#x5C55;&#x5F00;&#x51FA;&#x6765;&#x3002;&#x7B2C;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x5C55;&#x5F00;&#x591A;&#x6B21;&#xFF0C;&#x800C;&#x540E;&#x8005;&#x53EA;&#x5C55;&#x5F00;&#x4E00;&#x6B21;&#x3002;&#x6BD4;&#x5982;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x628A; <code>unless</code> &#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x4ECE;&#x524D;&#x9762;&#x7684;&#x65B9;&#x6848;&#x4FEE;&#x6539;&#x5982;&#x4E0B;&#xFF1A;  </p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">OurMacro</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">defmacro</span> <span class="hljs-title">unless</span></span>(expr, <span class="hljs-symbol">do:</span> block) <span class="hljs-keyword">do</span>
    <span class="hljs-keyword">quote</span> <span class="hljs-keyword">do</span>
      if !unquote(expr), <span class="hljs-symbol">do:</span> unquote(block)
    <span class="hljs-keyword">end</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>

require OurMacro

quoted =
  <span class="hljs-keyword">quote</span> <span class="hljs-keyword">do</span>
    OurMacro.<span class="hljs-keyword">unless</span>(<span class="hljs-keyword">true</span>, <span class="hljs-symbol">do:</span> <span class="hljs-string">&quot;Hi&quot;</span>)
  <span class="hljs-keyword">end</span>
</code></pre>
<pre><code class="lang-elixir">iex&gt; quoted |&gt; Macro.expand_once(__ENV_<span class="hljs-number">_</span>) |&gt; Macro.to_string |&gt; IO.puts
if(!<span class="hljs-keyword">true</span>) <span class="hljs-keyword">do</span>
  <span class="hljs-string">&quot;Hi&quot;</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x5982;&#x679C;&#x6211;&#x4EEC;&#x4F7F;&#x7528; <code>Macro.expand/2</code> &#x6765;&#x8FD0;&#x884C;&#x540C;&#x6837;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x6700;&#x7EC8;&#x7684;&#x7ED3;&#x679C;&#x5219;&#x76F8;&#x5F53;&#x6709;&#x8DA3;&#xFF1A;  </p>
<pre><code class="lang-elixir">iex&gt; quoted |&gt; Macro.expand(__ENV_<span class="hljs-number">_</span>) |&gt; Macro.to_string |&gt; IO.puts
<span class="hljs-keyword">case</span>(!<span class="hljs-keyword">true</span>) <span class="hljs-keyword">do</span>
  x <span class="hljs-keyword">when</span> x <span class="hljs-keyword">in</span> [<span class="hljs-keyword">false</span>, <span class="hljs-keyword">nil</span>] -&gt;
    <span class="hljs-keyword">nil</span>
  <span class="hljs-number">_</span> -&gt;
    <span class="hljs-string">&quot;Hi&quot;</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x6216;&#x8BB8;&#x4F60;&#x8FD8;&#x8BB0;&#x5F97;&#xFF0C;&#x6211;&#x4EEC;&#x66FE;&#x7ECF;&#x63D0;&#x8FC7;&#xFF0C;<code>if</code> &#x5728; Elixir &#x91CC;&#x9762;&#x4E5F;&#x662F;&#x4E00;&#x4E2A;&#x5B8F;&#x3002;&#x5728;&#x8FD9;&#x91CC;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x5B83;&#x5E95;&#x4E0B;&#x7684; <code>case</code> &#x8868;&#x8FBE;&#x5F0F;&#x88AB;&#x6269;&#x5C55;&#x51FA;&#x6765;&#x4E86;&#x3002;  </p>
<h3 id="&#x79C1;&#x6709;&#x5B8F;">&#x79C1;&#x6709;&#x5B8F;</h3>
<p>&#x5C3D;&#x7BA1;&#x4E0D;&#x5E38;&#x4F7F;&#x7528;&#xFF0C;Elixir &#x8FD8;&#x652F;&#x6301;&#x79C1;&#x6709;&#x5B8F;&#x3002;&#x79C1;&#x6709;&#x5B8F;&#x901A;&#x8FC7; <code>defmacrop</code> &#x5B9A;&#x4E49;&#xFF0C;&#x53EA;&#x80FD;&#x5728;&#x5B9A;&#x4E49;&#x7684;&#x6A21;&#x5757;&#x4E2D;&#x88AB;&#x8C03;&#x7528;&#x3002;&#x79C1;&#x6709;&#x5B8F;&#x5FC5;&#x987B;&#x5728;&#x8C03;&#x7528;&#x5B83;&#x7684;&#x4EE3;&#x7801;&#x4E4B;&#x524D;&#x5B9A;&#x4E49;&#x3002;</p>
<h3 id="&#x5B8F;&#x6E05;&#x6D01;">&#x5B8F;&#x6E05;&#x6D01;</h3>
<p>&#x5B8F;&#x6E05;&#x6D01;&#x662F;&#x6307;&#xFF1A;&#x5B8F;&#x548C;&#x8C03;&#x7528;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x4EA4;&#x4E92;&#x7684;&#x8FC7;&#x7A0B;&#x3002;&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;Elixir &#x4E2D;&#x7684;&#x5B8F;&#x662F;&#x6D01;&#x51C0;&#x7684;&#xFF1A;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x5B83;&#x4E0D;&#x4F1A;&#x548C;&#x8C03;&#x7528;&#x8005;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x51B2;&#x7A81;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">defmacro</span> <span class="hljs-title">hygienic</span></span> <span class="hljs-keyword">do</span>
    <span class="hljs-keyword">quote</span> <span class="hljs-symbol">do:</span> val = -<span class="hljs-number">1</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>

iex&gt; require Example
<span class="hljs-keyword">nil</span>
iex&gt; val = <span class="hljs-number">42</span>
<span class="hljs-number">42</span>
iex&gt; Example.hygienic
-<span class="hljs-number">1</span>
iex&gt; val
<span class="hljs-number">42</span>
</code></pre>
<p>&#x4F46;&#x662F;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x5E0C;&#x671B;&#x5728;&#x5B8F;&#x91CC;&#x9762;&#x4FEE;&#x6539; <code>val</code> &#x53D8;&#x91CF;&#x5462;&#xFF1F;&#x4F7F;&#x7528; <code>var!/2</code> &#x53EF;&#x4EE5;&#x8BA9;&#x67D0;&#x4E2A;&#x53D8;&#x91CF;&#x7684;&#x64CD;&#x4F5C;&#x53D8;&#x6210;&#x5BF9;&#x4E0A;&#x4E0B;&#x6587;&#x53D8;&#x91CF;&#x7684;&#x64CD;&#x4F5C;&#x3002;&#x5728;&#x6211;&#x4EEC;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x6DFB;&#x52A0;&#x53E6;&#x5916;&#x4E00;&#x4E2A;&#x4F7F;&#x7528; <code>var!/2</code> &#x7684;&#x5B8F;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">defmacro</span> <span class="hljs-title">hygienic</span></span> <span class="hljs-keyword">do</span>
    <span class="hljs-keyword">quote</span> <span class="hljs-symbol">do:</span> val = -<span class="hljs-number">1</span>
  <span class="hljs-keyword">end</span>

  <span class="hljs-function"><span class="hljs-keyword">defmacro</span> <span class="hljs-title">unhygienic</span></span> <span class="hljs-keyword">do</span>
    <span class="hljs-keyword">quote</span> <span class="hljs-symbol">do:</span> var!(val) = -<span class="hljs-number">1</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x7136;&#x540E;&#x6BD4;&#x8F83;&#x4E00;&#x4E0B;&#x5B83;&#x4EEC;&#x662F;&#x600E;&#x4E48;&#x548C;&#x4EE3;&#x7801;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x4EA4;&#x4E92;&#x7684;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; require Example
<span class="hljs-keyword">nil</span>
iex&gt; val = <span class="hljs-number">42</span>
<span class="hljs-number">42</span>
iex&gt; Example.hygienic
-<span class="hljs-number">1</span>
iex&gt; val
<span class="hljs-number">42</span>
iex&gt; Example.unhygienic
-<span class="hljs-number">1</span>
iex&gt; val
-<span class="hljs-number">1</span>
</code></pre>
<p>&#x4F7F;&#x7528; <code>var!/2</code>&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x64CD;&#x4F5C; <code>val</code> &#x53D8;&#x91CF;&#x800C;&#x4E0D;&#x9700;&#x8981;&#x5B83;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x4F20;&#x9012;&#x7ED9;&#x5B8F;&#x3002;&#x5B8F;&#x7684;&#x8FD9;&#x79CD;&#x4F7F;&#x7528;&#x65B9;&#x5F0F;&#x5E94;&#x8BE5;&#x5C3D;&#x5C11;&#x4F7F;&#x7528;&#xFF0C;&#x5F15;&#x5165; <code>var!/2</code> &#x4F1A;&#x589E;&#x52A0;&#x53D8;&#x91CF;&#x51B2;&#x7A81;&#x7684;&#x98CE;&#x9669;&#x3002;</p>
<h3 id="&#x7ED1;&#x5B9A;">&#x7ED1;&#x5B9A;</h3>
<p>&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x4ECB;&#x7ECD;&#x8FC7; <code>unquote/1</code> &#x7684;&#x7528;&#x6CD5;&#xFF0C;&#x4F46;&#x662F;&#x8FD8;&#x6709;&#x53E6;&#x5916;&#x4E00;&#x79CD;&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x5728;&#x4EE3;&#x7801;&#x4E2D;&#x63D2;&#x5165;&#x503C;&#xFF1A;&#x7ED1;&#x5B9A;&#x3002;&#x4F7F;&#x7528;&#x53D8;&#x91CF;&#x7ED1;&#x5B9A;&#xFF0C;&#x6211;&#x4EEC;&#x80FD;&#x591F;&#x5728;&#x5B8F;&#x4E2D;&#x591A;&#x6B21;&#x4F7F;&#x7528;&#x53D8;&#x91CF;&#xFF0C;&#x5E76;&#x4E14;&#x4FDD;&#x8BC1;&#x5B83;&#x4EEC;&#x53EA;&#x4F1A;&#x88AB;&#x8BA1;&#x7B97;&#x4E00;&#x6B21;&#xFF0C;&#x4ECE;&#x800C;&#x907F;&#x514D;&#x610F;&#x5916;&#x7684;&#x91CD;&#x65B0;&#x8BA1;&#x7B97;&#x3002;&#x8981;&#x4F7F;&#x7528;&#x7ED1;&#x5B9A;&#x53D8;&#x91CF;&#xFF0C;&#x6211;&#x4EEC;&#x5FC5;&#x987B;&#x4F20;&#x4E00;&#x4E2A;&#x5173;&#x952E;&#x5B57;&#x5217;&#x8868; <code>bind_quoted</code> &#x4F5C;&#x4E3A; <code>quote/2</code> &#x7684;&#x9009;&#x9879;&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x7406;&#x89E3; <code>bind_quote</code> &#x7684;&#x597D;&#x5904;&#x548C;&#x91CD;&#x65B0;&#x8BA1;&#x7B97;&#x7684;&#x95EE;&#x9898;&#xFF0C;&#x6211;&#x4EEC;&#x6765;&#x770B;&#x4E2A;&#x4F8B;&#x5B50;&#x3002;&#x521B;&#x5EFA;&#x4E00;&#x4E0B;&#x628A;&#x67D0;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x503C;&#x6253;&#x5370;&#x4E24;&#x904D;&#x7684;&#x5B8F;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">defmacro</span> <span class="hljs-title">double_puts</span></span>(expr) <span class="hljs-keyword">do</span>
    <span class="hljs-keyword">quote</span> <span class="hljs-keyword">do</span>
      IO.puts(unquote(expr))
      IO.puts(unquote(expr))
    <span class="hljs-keyword">end</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x6211;&#x4EEC;&#x628A;&#x5F53;&#x524D;&#x7684;&#x7CFB;&#x7EDF;&#x65F6;&#x95F4;&#x4F20;&#x9012;&#x8FC7;&#x53BB;&#xFF0C;&#x6765;&#x6D4B;&#x8BD5;&#x4E00;&#x4E0B;&#x521A;&#x5B9A;&#x4E49;&#x7684;&#x5B8F;&#xFF0C;&#x671F;&#x671B;&#x662F;&#xFF1A;&#x5B83;&#x628A;&#x5F53;&#x524D;&#x65F6;&#x95F4;&#x6253;&#x5370;&#x4E24;&#x904D;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; Example.double_puts(<span class="hljs-symbol">:os</span>.system_time)
<span class="hljs-number">1450475941851668000</span>
<span class="hljs-number">1450475941851733000</span>
</code></pre>
<p>&#x4E24;&#x6B21;&#x7684;&#x65F6;&#x95F4;&#x5C45;&#x7136;&#x4E0D;&#x4E00;&#x6837;&#xFF01;&#x4EC0;&#x4E48;&#x9B3C;&#xFF1F;&#x5BF9;&#x540C;&#x4E00;&#x4E2A;&#x8868;&#x8FBE;&#x5F0F;&#x591A;&#x6B21;&#x4F7F;&#x7528; <code>unquote/1</code> &#x4F1A;&#x91CD;&#x65B0;&#x8BA1;&#x7B97;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x503C;&#xFF0C;&#x8FD9;&#x4F1A;&#x5BFC;&#x81F4;&#x610F;&#x60F3;&#x4E0D;&#x5230;&#x7684;&#x9519;&#x8BEF;&#x3002;&#x6211;&#x4EEC;&#x6765;&#x66F4;&#x65B0;&#x4E00;&#x4E0B;&#x4F8B;&#x5B50;&#xFF0C;&#x770B;&#x770B;&#x4F7F;&#x7528; <code>bind_quoted</code> &#x7684;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Example</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">defmacro</span> <span class="hljs-title">double_puts</span></span>(expr) <span class="hljs-keyword">do</span>
    <span class="hljs-keyword">quote</span> <span class="hljs-symbol">bind_quoted:</span> [<span class="hljs-symbol">expr:</span> expr] <span class="hljs-keyword">do</span>
      IO.puts(expr)
      IO.puts(expr)
    <span class="hljs-keyword">end</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>

iex&gt; require Example
<span class="hljs-keyword">nil</span>
iex&gt; Example.double_puts(<span class="hljs-symbol">:os</span>.system_time)
<span class="hljs-number">1450476083466500000</span>
<span class="hljs-number">1450476083466500000</span>
</code></pre>
<p>&#x4F7F;&#x7528; <code>bind_quoted</code> &#x548C;&#x6211;&#x4EEC;&#x9884;&#x671F;&#x7684;&#x7ED3;&#x679C;&#x662F;&#x4E00;&#x81F4;&#x7684;&#xFF1A;&#x5F53;&#x524D;&#x7684;&#x65F6;&#x95F4;&#x88AB;&#x6253;&#x5370;&#x4E24;&#x6B21;&#x3002;</p>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x5B66;&#x5B8C;&#x4E86; <code>quote/2</code>&#xFF0C;<code>unquote/1</code> &#x548C; <code>defmacro/2</code>&#xFF0C;&#x8FD9;&#x4E9B;&#x5DE5;&#x5177;&#x5DF2;&#x7ECF;&#x8DB3;&#x591F;&#x6211;&#x4EEC;&#x6309;&#x7167;&#x9700;&#x8981;&#x6269;&#x5C55; Elixir &#x4E86;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="otp-distribution.html" class="navigation navigation-prev " aria-label="Previous page: OTP 分布式">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="umbrella-projects.html" class="navigation navigation-next " aria-label="Next page: Umbrella Projects">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"version":"1.0.2","title":"元编程","level":"2.8","depth":1,"next":{"title":"Umbrella Projects","level":"2.9","depth":1,"path":"lessons/advanced/umbrella-projects.md","ref":"lessons/advanced/umbrella-projects.md","articles":[]},"previous":{"title":"OTP 分布式","level":"2.7","depth":1,"path":"lessons/advanced/otp-distribution.md","ref":"lessons/advanced/otp-distribution.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"lessons/advanced/metaprogramming.md","mtime":"2019-11-05T02:25:51.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-11-05T03:01:22.004Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

